(function () {
    /*
     * ScriptWord
     * 脚本分词器
     * */
    var WordVO = kiscompress.WordVO;
    var WordType = kiscompress.WordType;
    var ConstantKind = kiscompress.ConstantKind;
    function ScriptWord() {

        this.exoptions = []; //操作符
        this.switchs = []; //长段文字开关，例如:注释、字符串、正则
        this.whitespace = []; //空白符，注意：需要记录空白符才添加内容
        this.connectwords = []; //非计算符的连接符,例如:大括号、中括号、小括号
        this.keywords = [];
    }

    ScriptWord.KEYWORD = 0x1001;
    ScriptWord.CONNECTWORD = 0x1002;

    var b = ScriptWord.prototype;
    /**
     * 提取关键字
     * @param	script
     * @return
     */
    b.getWords = function (script) {
        var l = script.length;
        var wordContent = '';
        var word = null;
        var row = 0; //当前分析累加的行号
		var anchor = 0;
        var wordList = [];
        var lastchar = "";
        var xg_c = 0;
        var lastSwitch = null;
        var exoptionsLength = this.exoptions.length;
        var whitespaceLength = this.whitespace.length;
        for (var i = 0; i < l; i++) {
            var char = script.charAt(i);
            var wordSum = false; //是否将单词累加
            var nextstep = true;
            var crlf = false; //回车
            var markRow = row;
			var markPosition = i;
            //检测长文段
            if (!lastSwitch) {
                var jl = this.switchs.length;
                for (var j = 0; j < jl; j++) {
                    var swh = this.switchs[j];
                    var stakey = swh.staKey;
                    if (stakey == script.substr(i, stakey.length)) {
                        swh.sta = i;

                        //检测是否符合成立条件
                        if (swh.callback != null) {
                            var result = swh.callback.apply(swh.target, [swh.switchID, 0, wordContent, script, i + stakey.length - 1, wordList]);
                            if (result) {
                                if (!isNaN(result.offset)) {
                                    i += result.offset;
                                }
                                if (result.wordContent != undefined) {
									//没有记录在案的关键字
									if(wordList && wordList.length && wordContent !=wordList[wordList.length-1].content)
									{
										var word3 = this.createWordVO(wordContent,markRow,anchor);
										wordList.push(word3);
										word3 = null;
									}
                                    wordContent = result.wordContent;
                                }
                                if (result.status == 1) {
                                    //错误状态略过
                                    break;
                                }
                            }
                        }

                        //开始
                        lastSwitch = swh;
                        nextstep = false;
                        xg_c = 0;
                        if (stakey.length > 1) {
                            i += stakey.length - 1;
                        }
                        break;
                    }
                }
            } else {
                swh = lastSwitch;
                var ck = swh.changeKey; //切换忽略结束符
                if (ck) {
                    //排除转义
                    var ckl = ck.length;
                    var cur_ck = script.substr(i, ckl);
                    var last_ck = script.substr(i - ckl, ckl);
                    if ((cur_ck == ck && last_ck == ck && xg_c == 1) ||
                        (cur_ck != ck && last_ck != ck)) {
                        xg_c = 0;
                    } else if (cur_ck == ck && (last_ck != ck || xg_c == 0)) {
                        xg_c = 1;
                    }
                }
                if (xg_c == 0) {
                    if (swh.callback != null) {
                        //是否需要中断
                        result = swh.callback.apply(swh.target, [swh.switchID, 1, wordContent, script, i, wordList]);
                        if (result) {
                            if (!isNaN(result.offset)) {
                                i += result.offset;
                            }
                            if (result.wordContent != undefined) {
                                wordContent = result.wordContent;
                            }
                            if (result.status == 1) {
                                //错误状态
                                //多文段断裂，重新返回起始点，当普通操作符检测
                                i = swh.sta + 1;
                                wordContent = '';
                                lastSwitch = null;
                                swh = null;
                                nextstep = true;

                            }
                        }
                    }
                    if (swh) {
                        var endkeys = swh.endKeys;
                        if (endkeys) {
                            jl = endkeys.length;
                            for (j = 0; j < jl; j++) {
                                var endkey = endkeys[j];
                                var fendk = endkey == script.substr(i, endkey.length);
                                if (fendk || i == l - 1) {
                                    //结束
                                    if (fendk && swh.callback != null) {
                                        result = swh.callback.apply(swh.target, [swh.switchID, 2, wordContent, script, i + endkey.length - 1, wordList]);
                                        if (result) {
                                            if (!isNaN(result.offset)) {
                                                i += result.offset;
                                            }
                                            if (result.wordContent != undefined) {
                                                wordContent = result.wordContent;
                                            }
                                            if (result.status == 1) {
                                                //错误状态略过
                                                break;
                                            }
                                        }
                                    }
                                    nextstep = false;
                                    swh.end = i;
                                    if (swh.keep) {
                                        word = new WordVO();
                                        word.content = wordContent;
                                        word.type = swh.type;
                                        word.kind = swh.kind;
                                        word.row = markRow;
										word.start = swh.sta;
                                    } else {
                                        //trace(wordContent);
                                    }
                                    wordContent = '';
                                    if (endkey.length > 1) {
                                        i += endkey.length - 1;
                                    }
                                    lastSwitch = null;
                                    break;
                                }
                            }
                        }
                    }

                }
                if (lastSwitch) {
                    nextstep = false;
                    wordSum = true;
                }
            }

            if (nextstep) {
                //检测操作符
                jl = exoptionsLength;
                for (j = 0; j < jl; j++) {
                    var jn = this.exoptions[j];
                    var jc = jn.charAt(0);
                    var jnl = jn.length;
                    if (jc == char) {
                        if (script.substr(i, jnl) == jn) {
                            //跳过下个检测
                            word = new WordVO();
                            word.type = WordType.OPTION;
                            word.kind = this.connectwords.indexOf(word.content) != -1 ? ScriptWord.CONNECTWORD : 0;
                            word.content = jn;
                            word.row = row;
							word.start = i;
                            nextstep = false;
                            i += jnl - 1;
                            break;
                        }
                    }
                }

                jl = whitespaceLength;
                for (j = 0; j < jl; j++) {
                    jn = this.whitespace[j];
                    jc = jn.charAt(0);
                    jnl = jn.length;
                    if (jc == char) {
                        if (script.substr(i, jnl) == jn) {
                            //跳过下个检测
                            word = new WordVO();
                            word.type = WordType.WHITESPACE;
                            word.content = jn;
                            word.row = row;
							word.start = i;
                            nextstep = false;
                            i += jnl - 1;
                            break;
                        }
                    }
                }

                if (nextstep) {
                    switch (char) {
                    case ' ':
                    case '\t':
                        //直接忽略
                        break;
                    case '\n':
                        row++;
                        crlf = true;
                        break;
                    case '\r':
                        if (i + 1 < l && script.charAt(i + 1) != "\n") {
                            row++;
                            crlf = true;
                        }
                        break;
                    default:
                        wordSum = true;
                    }
                }
            }
            if (wordSum) {
				if(wordContent.length==0)
				{
					anchor = markPosition;
				}
                //标识符
                wordContent += char;
            }

            if ((!wordSum || i == l - 1) && wordContent) {
                var word2 = this.createWordVO(wordContent,markRow,anchor);
				wordList.push(word2);
                wordContent = '';
            }

            if (word) {
                wordList.push(word);
            }

            if (crlf && wordList.length) {
                //标记回车记录
                wordList[wordList.length - 1].nextCRLF = true;
            }

            lastchar = char;
            word = null;
        }
        return wordList;
    }
	
	b.createWordVO = function(wordContent,markRow,anchor)
	{
		var word2 = new WordVO();
		word2.type = WordType.SYMBOL;
		word2.content = wordContent;
		word2.row = markRow;
		word2.start = anchor;
		if (this.keywords.indexOf(wordContent) != -1) {
			word2.kind = ScriptWord.KEYWORD;
		} else if (wordContent == "false") {
			word2.type = WordType.CONSTANT;
			word2.kind = ConstantKind.CONSTANT_False;
		} else if (wordContent == "true") {
			word2.type = WordType.CONSTANT;
			word2.kind = ConstantKind.CONSTANT_True;
		} else if (wordContent == "NaN") {
			word2.type = WordType.CONSTANT;
			word2.kind = ConstantKind.CONSTANT_Double;
		} else if (wordContent == "null") {
			word2.type = WordType.CONSTANT;
			word2.kind = ConstantKind.CONSTANT_Null;
		} else if (wordContent == "undefined") {
			word2.type = WordType.CONSTANT;
			word2.kind = ConstantKind.CONSTANT_Undefined;
		} else {
			var val = Number(wordContent)
				if (!isNaN(val)) {
					word2.type = WordType.CONSTANT;
					if (parseInt(val) == val) {
						word2.kind = ConstantKind.CONSTANT_Int;
					} else {
						word2.kind = ConstantKind.CONSTANT_Double;
					}
					//trace("val:" + val);
				}
		}
		return word2;
	}

    kiscompress.ScriptWord = ScriptWord;
})();
